<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Letter;
use App\Services\LetterService;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Mail;

class LetterController extends Controller
{

    public function __construct(LetterService $letter){
        $this->letter = $letter;
    }


    public function writeLetter(Request $request){
        if($request->ajax()){
            try {
                $this->letter->writeLetterService($request);
                return activeSuccess('','信件暂存成功，到达指定时间自动发出');
            }catch (\Exception $exception){
                Log::info('写信提交错误：'.$exception->getMessage().' 行号：'.$exception->getLine());
                return activeFail($exception->getLine(),'系统错误：'. $exception->getMessage());
            }
        }

        $placeholder = [
            '给未来的自己写一封信',
            '嗨，我是来自十年前的你，你现在还好吗',
            '一封来自'.date('Y年m月d日').'的信',
            '给十年后的自己写一封信',
            '来自'.date('Y').'年的时光邮件，请查收',
        ];
        $placeholder = $placeholder[array_rand($placeholder)];
        $dateTip = [
            [
                'name' => '100天',
                'val' =>[
                    'DD'=> 100
                ]
            ],
            [
                'name' => '1000天',
                'val' =>[
                    'DD'=> 1000
                ]
            ],
            [
                'name' => '一年',
                'val' =>[
                    'DD'=> 365
                ]
            ],
            [
                'name' => '三年',
                'val' =>[
                    'MM'=> 36
                ]
            ],
            [
                'name' => '五年',
                'val' =>[
                    'MM'=> 60
                ]
            ],
            [
                'name' => '十年',
                'val' =>[
                    'MM'=> 120
                ]
            ],
            [
                'name' => '高考',
                'val' =>[
                    'DD'=>  getDateDays(date('Y-m-d'),'2024-06-07')
                ]
            ],
            [
                'name' => '2025年元旦',
                'val' =>[
                    'DD'=>  getDateDays(date('Y-m-d'),'2025-01-01')
                ]
            ],
            [
                'name' => '2025年情人节',
                'val' =>[
                    'DD'=> getDateDays(date('Y-m-d'),'2025-02-14')
                ]
            ],
        ];
        $dateTip = json_encode($dateTip);
        return view('mail.post',compact('placeholder','dateTip'));
    }



    public function sendLetter(Request $request){

        try {
            $email = Letter::find(1);
            Mail::send('email.email', ['lettet' => $email], function ($message) use ($email) {
                $to = $email->email;
                $message->to($to)->subject($email->title . "—来自" . $email->username . "在" . getYMD($email->created_at) . "写给你的信【时光驿站】");
            });
            Log::info('邮件ID:'.$email->id.'【'.$email->tittle.'】 '.date('Y-m-d H:i:s').'发送成功');
            dd(6666);
        }catch (\Exception $exception){
            Log::info('写信提交错误：'.$exception->getMessage().' 行号：'.$exception->getLine());
            return activeFail($exception->getLine(),'系统错误：'. $exception->getMessage());
        }
    }


}
